var pageNo=1;
var pageSize=10;
var flag=1;
var shelfFlag=1;
var deleteFlag=1;
var couponId=[];
var couponStr="";
var couponOpenStatus="";
var status=0;

$(document).ready(function () {
    showList();
});
//列表集
function showList(){
    if(flag!==1){
        return;
    }
    flag=0;
    ajax({
        url:"coupon/queryCouponBusinessListPage",
        data:{
            pageNo:pageNo,
            pageSize:pageSize,
            status:status
        },
        success:function(data){
            
            if(data.success){
                var count=data.data.count;
                handleCount(count);
                var list=data.data.list;
                createPageInfo(list);
                // var coupon=list.list;
                var html=template("template",list);
                document.getElementById('tbody').innerHTML = html;
                previewImg();
                $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green'
                });
                flag=1;
            }else{
                flag=1;
                layer.msg(data.message)
            }
        }
    });
}
////审核状态
$('.verify>a').on("click",function(){
    if($(this).hasClass("couponCount")){
        status=0;
    } else if($(this).hasClass("couponOpenCount")){
        status=1;
    } else if($(this).hasClass("couponCloseCount")){
        status=2;
    } else if($(this).hasClass("couponDeleteCount")){
        status=3;
    }
    pageNo=1;
    showList();
});
//修改的商品集
function choose(){
    couponId=[];
    couponStr="";
    var checked=$('.i-checks').children();
    for(var i=0;i<$(checked).length;i++){
        if($($(checked)[i]).hasClass("checked")){
            couponId.push($($(checked)[i].parentNode.parentNode.parentNode).attr("data-id"));
        }
    }
    couponStr = couponId.join(",");
    
}
$("#signing").click(function(){
    $("#handle").val("1").trigger("change");
    $("#apply").trigger("click");
});
$("#notSigning").click(function(){
    $("#handle").val("0").trigger("change");
    $("#apply").trigger("click");
});
//上下架
$("#handle").change(function () {
    couponOpenStatus=this.value;
});
//应用
$("#apply").click(function(){
    if(couponOpenStatus===""){
        layer.msg("请选择批量操作类型");
        return;
    }
    if(shelfFlag!==1){
        return;
    }
    choose();
    if(isNULL(couponStr)){
        layer.msg("请选择要操作的对象");
        return;
    }
    shelfFlag=0;
    ajax({
        url:"coupon/updateCouponStatusBatch",
        data:{
            couponIds:couponStr,
            couponOpenStatus:couponOpenStatus
        },
        success:function(data){
            
            if(data.success){
                showList();
                shelfFlag=1;
                layer.msg(data.message);
            }else{
                shelfFlag=1;
                layer.msg(data.message)
            }
        }
    });
});

//全选/全不选
$("#checkAll").click(function () {
    if ($(this).text() == "全选") {
        $('.i-checks').iCheck('check');
        $(this).text("不选");
    } else {
        $('.i-checks').iCheck('uncheck');
        $(this).text("全选")
    }
});
$("#triggerCheck").on("ifClicked",function(){
    $("#checkAll").trigger("click");
});

//处理优惠券数量
function handleCount(count){
    $("#couponCount").html(count.couponCount);
    $("#couponOpenCount").html(count.couponOpenCount);
    $("#couponCloseCount").html(count.couponCloseCount);
    $("#couponDeleteCount").html(count.couponDeleteCount);
}
//改变页面显示数量
$("#pageSizeInfo").change(function () {
    $("#pageSizeInfo").blur();
    if (flag == 1) {
        return
    }
    pageSize = this.options[this.options.selectedIndex].value;
    pageNo=1;
    showList();
});
//编辑和删除
$("#tbody").on("click",".edit",function () {
//店铺id和name
    var tr=this.parentNode.parentNode;
    var couponMsg=$(tr).attr("data-msg");
    setStorage("couponMsg",couponMsg);
    window.location.href="couponAdd.html";
}).on("click",".delete",function () {
    var tr=this.parentNode.parentNode;
    var couponId=$(tr).attr("data-id");
    var businessId=$(tr).attr("data-biid");
    changeCoupon(couponId,businessId,1);
}).on("click",".restore",function () {
    var tr=this.parentNode.parentNode;
    var couponId=$(tr).attr("data-id");
    var businessId=$(tr).attr("data-biid");
    changeCoupon(couponId,businessId,0);
});
//删除恢复优惠券
function changeCoupon(couponId,businessId,isDelete) {
    if(deleteFlag!==1){
        return;
    }
    deleteFlag=0;
    ajax({
        url:"coupon/updateCouponTrash",
        data:{
            couponId:couponId,
            businessId:businessId,
            isDelete:isDelete
        },
        success:function(data){
            
            if(data.success){
                showList();
                deleteFlag=1;
            }else{
                deleteFlag=1;
            }
            layer.msg(data.message);
        }
    });
}
//创建页码信息
function createPageInfo(list) {
    $("#startRow").text(list.startRow);
    $("#endRow").text(list.endRow);
    $("#totalPage").text(list.total);
    pageNum=list.pages;
    var temp = '';
    for (var i = 1; i < list.pages + 1; i++) {
        temp += '<option value="' + i + '">' + i + '</option>'
    }
    $("#pageInfo").html(temp)
        .val(list.pageNum)
        .change(function () {
            $("#pageInfor").blur();
            if (flag !== 1) {
                return
            }
            pageNo = this.options[this.options.selectedIndex].value;
            showList();
        });
    $("#firstPage").click(function () {
        if (pageNo!= 1) {
            if (flag !== 1) {
                return
            }
            pageNo = 1;
            showList();
        }
    });
    $("#lastPage").click(function () {
        if (pageNo < pageNum) {
            if (flag !== 1) {
                return
            }
            pageNo = pageNum;
            showList();
        }
    });
    $("#nextPage").click(function () {
        if (pageNo < pageNum) {
            if (flag !== 1) {
                return
            }
            pageNo++;
            showList();
        }
    });
    $("#previousPage").click(function () {
        if (pageNo > 1) {
            if (flag !== 1) {
                return
            }
            pageNo--;
            showList();
        }
    });
    $("#pageSizeInfo").change(function () {
        $("#pageSizeInfo").blur();
        if (flag !== 1) {
            return
        }
        pageSize = this.options[this.options.selectedIndex].value;
        showList();
    });

}